var netcore = require('../../../utils/network.js')
var tripleDes = require('../../../utils/CKCrypto.js')
var cache = require('../../../utils/Cache.js')
//类型加载
function loadCompositionTypes(callBack) {
  var _cacheKey = "articleTypes";
  var cacheList = cache.getValue(_cacheKey, null)
  if (cacheList == null) {
    netcore.requestPost("composition/getTypes.do", new Map(), function(success) {
      if (success.length > 0) {
        cache.save(_cacheKey, success, 24 * 60 * 60)
      }
      callBack(success)
    }, function(code, errMsg) {
      wx.showToast({
        title: errMsg,
        icon: 'none'
      })
    })
  } else {
    callBack(cacheList)
  }
}
/**
 * gradeLevel 年级 必传
 */
function loadIndexList(gradeLevel, type, theme, callBack) {
  var params = new Map();
  params.set("grade", tripleDes.enCode(gradeLevel))
  if (type != null && typeof(type) != 'undefined') {
    params.set("type", type)
  }
  if (theme != null && typeof(theme) != 'undefined') {
    params.set("theme", theme)
  }
  netcore.requestPost("composition/getIndex.do", params, function(success) {
    success.boutiqueList.forEach(function(item, index) {
      item.id = tripleDes.deCode(item.id)
      item.title = tripleDes.deCode(item.title)
    })
    success.allList.forEach(function(item, index) {
      item.id = tripleDes.deCode(item.id)
      item.title = tripleDes.deCode(item.title)
    })
    // if (success.boutiqueList.length > 0 && success.allList.length > 0) {
    //   cache.save(cacheKey, success, 24 * 60 * 60)
    // }
    callBack(success.boutiqueList, success.allList)
  }, function(code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon: 'none'
    })
  })
  // var cacheKey="articleIndex";
  // var cacheList = cache.getValue(cacheKey,null)
  // if (cacheList==null){

  // }else{
  //   callBack(cacheList.boutiqueList, cacheList.allList)
  // }
}
/***
 * 加载更多
 * gradeLevel:年级 （必传）
 * boutique：是否精品0/1 （必传）
 * type：题材
 * theme：类型
 * title：标题
 * hot：热度排序 1
 * newer：最新
 */
function getMoreList(gradeLevel, boutique, type, theme, title, tag,hot, newer, currentPage, pageSize,callBack) {
  var params = new Map();
  params.set("grade", tripleDes.enCode(gradeLevel))
  params.set("boutique", boutique)
  if (type != null && typeof(type) != 'undefined') {
    params.set("type", tripleDes.enCode(type))
  }
  if (theme != null && typeof(theme) != 'undefined') {
    params.set("theme", theme)
  }
  if (title != null && typeof(title) != 'undefined') {
    params.set("title", tripleDes.enCode(title))
  }
  if (hot != null && typeof(hot) != 'undefined') {
    params.set("hot", hot)
  }
  if (newer != null && typeof(newer) != 'undefined') {
    params.set("newer", newer)
  }
  params.set("currentPage", currentPage)
  params.set("pageSize", pageSize)
  netcore.requestPost("composition/getMore.do", params, function(success) {
    if(success){
      success.forEach(function (item, index) {
        item.id = tripleDes.deCode(item.id)
        item.title = tripleDes.deCode(item.title)
      })
    }
    callBack(success)
  }, function(code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon: 'none'
    })
  })
}

function loadDetail(_no,callBack) {
  var params = new Map();
  params.set("id", tripleDes.enCode(_no))
  netcore.requestPost("composition/getDetail.do", params, function (success) {
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon: 'none'
    })
  })
}
function rePort(_no, _ak,_action,callBack) {
  var params = new Map();
  params.set("id", tripleDes.enCode(_no))
  params.set("model",_action)
  params.set("ak", _ak)
  netcore.requestPostNoLoading("composition/rePortArticle.do", params, function (success) {
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon: 'none'
    })
  })
}
module.exports = {
  loadCompositionTypes: loadCompositionTypes,
  loadIndexList: loadIndexList,
  getMoreList: getMoreList,
  loadDetail: loadDetail,
  rePort: rePort
}